package tk.mystudio.util;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ResourceUtil {

	/**
	 * 几个约定：
	 * 1、常量类包名中含有"constant"一词
	 * @param args
	 */
	public static void main(String[] args) {
		String baseClassNames[] = new String[]{"tk.mystudio.permission.bean.Account",
				"tk.mystudio.permission.constant.AccountState"};
		List<String> classNameList = new ArrayList<String>();
		try {
			for (int i = 0; i < baseClassNames.length; i++) {
				Class clazz = Class.forName(baseClassNames[i]);
				String packageName = clazz.getPackage().getName();
				File files[] = new File(clazz.getResource(".").getFile())
						.listFiles();
				if (files != null && files.length > 0) {
					for (File file : files) {
						String className = packageName + "."
								+ file.getName().replace(".class", "");
						classNameList.add(className);
					}
				}
			}
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < classNameList.size(); i++) {
				Class clazz = Class.forName(classNameList.get(i));
				String className = clazz.getSimpleName();
				String rclassName = className;
				boolean isConstantClass = true;
				if(!clazz.getPackage().getName().contains("constant")) {
					isConstantClass = false;
					rclassName = className.substring(0, 1).toLowerCase()+className.substring(1);
				}
				sb.append("#"+className+"\n");
				Field[] fields = clazz.getDeclaredFields();
				for (Field field : fields) {
					if (field.getName().equals("serialVersionUID")) {
						continue;
					}
					sb.append(rclassName + "." + field.getName() + "=\n");
					if(isConstantClass) {
						sb.append(rclassName + "." + field.get(null) + "=\n");
					}
				}
				sb.append("\n");
			}
			System.out.println(sb.toString());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

}
